package org.jeecg.modules.flow.callback.move;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.mapper.WmsMoveReceiptMaterialMapper;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.transfer.vo.WmsMoveReceiptPage;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 纵剪叫纸筒提交 回调Bean
 *
 * @author lumingyi
 * @version 1.0.0
 * @since 2023-11-15
 */
@RequiredArgsConstructor
@Component
public class Y14MoveSubmitBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsMoveReceiptService moveReceiptService;
    private final IWmsRepertoryService wmsRepertoryService;
    private final WmsMoveReceiptMaterialMapper moveReceiptMaterialMapper;

    /**
     * 业务逻辑 纵剪叫纸筒提交
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(WmsMoveReceiptPage.class);
        WmsMoveReceiptPage wmsMoveReceiptPage = (WmsMoveReceiptPage) baseFlowDto.getData();
        String receiptId = wmsMoveReceiptPage.getId();
        WmsMoveReceipt wmsMoveReceipt = new WmsMoveReceipt();
        BeanUtils.copyProperties(wmsMoveReceiptPage, wmsMoveReceipt);
        if (StrUtil.isEmpty(receiptId)) {
            receiptId = moveReceiptService.saveMain(wmsMoveReceipt, wmsMoveReceiptPage.getWmsMoveReceiptMaterialList());
        } else {
            List<WmsMoveReceiptMaterial> materials = moveReceiptMaterialMapper.selectList(new LambdaQueryWrapper<WmsMoveReceiptMaterial>()
                    .eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, wmsMoveReceipt.getId()));
            moveReceiptService.updateMain(wmsMoveReceipt, materials);
        }
        // 库存校验
        wmsRepertoryService.moveSubmit(receiptId);

        WmsMoveReceipt moveReceipt = moveReceiptService.getById(receiptId);
        if(Objects.isNull(moveReceipt)){
            throw new JeecgBootException("移库单id错误");
        }
        // 状态变为待执行
        moveReceiptService.update(new LambdaUpdateWrapper<WmsMoveReceipt>()
                .eq(WmsMoveReceipt::getId, receiptId)
                .set(WmsMoveReceipt::getDocumentStatus, DocumentStatusEnum.COMMIT.getCode()));
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        baseFlowDto.setInfoId(receiptId);
    }
}
